import { Action, ActionKey, loading, actionManager, ProjectValidType, PanelType, SaveProjectAction, autoMarkCurrentDiagram, freshOneVliadResult, freshProjectData } from "@/mdesign-lib/mdesign-app.umd.js";

export class ValidateProjectAction extends Action {
  keyName = ActionKey.validateProject;

  desc = "模型校验，校验整个项目中的模型问题";

  onSysMenuClick() {
    this.run();
  }
  async run(item: { isShowNotify?: boolean; type?: ProjectValidType; clearStep?: boolean } = {}) {
    let option = { ...{ isShowNotify: true, type: ProjectValidType.projectValid, clearStep: false }, ...item };
    const project = app.activeProject;
    if (!project) return;
    // if (option.isShowLoading) {
    //   loading.popup("校验中...");
    // }
    try {
      let data = await freshOneVliadResult(option.type);
      if (app.activeGraph) {
        autoMarkCurrentDiagram(app.activeGraph);
      }
      if (data?.length) {
        project.ui.addPanelTab(PanelType.ResultTab);
      } else if (option.isShowNotify && option.type === ProjectValidType.projectValid) {
        app.$notify({
          title: "校验完成",
          type: "success",
          message: "没有发现问题"
        });
        loading.close();
        return true;
      }
      if (option.clearStep) {
        const saveProjectAction = actionManager.getAction(ActionKey.saveProject) as SaveProjectAction;
        await saveProjectAction.run(project.id, false, false);
        setTimeout(() => {
          project.ui.stepStatus.clear();
        }, 200);
      }
      // 刷新当前项目
      if (option.type !== ProjectValidType.UnlawfulShapesValid) {
        await freshProjectData({});
      }
    } catch (error) {
      console.log("error", error);
      loading.close();
      project.ui.modelValidationData = [];
      app.$notify({
        title: "校验错误",
        type: "error",
        message: "校验错误"
      });
    }
    loading.close();
  }
}
